/**
 * 
 */
package com.Hib.Recipes;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.stat.Statistics;

import com.Hib.Domain.Book;
import com.Hib.Util.SessionManager;

/**
 * @author Ignatiues charles arun
 * 
 */
public class BookDAO
	{

		public static void main(String args[])
			{
				Book[] objBook = new Book[3];
				java.util.Date date = new Date();
				Calendar rightNow = Calendar.getInstance();
				Book objBook1 = new Book();
				objBook1.setIsbn("234");
				objBook1.setName("JAVA");
				objBook1.setPrice(111);
				objBook1.setPublishDate(rightNow.getTime());

				objBook[0] = new Book();
				objBook[0].setIsbn("120");
				objBook[0].setName("Hibernate Recepies");
				objBook[0].setPublishDate(rightNow.getTime());
				objBook[0].setPrice(300);

				objBook[1] = new Book();
				objBook[1].setIsbn("124");
				objBook[1].setName("Spring Web Recepies");
				objBook[1].setPublishDate(rightNow.getTime());
				objBook[1].setPrice(360);

				objBook[2] = new Book();
				objBook[2].setIsbn("125");
				objBook[2].setName("Head First JAVA");
				objBook[2].setPublishDate(rightNow.getTime());
				objBook[2].setPrice(400);

				// objBook[3] = new Book();
				// objBook[3].setIsbn("126");
				// objBook[3].setName("Design Pattern");
				// objBook[3].setPublishDate(rightNow.getTime());
				// objBook[3].setPrice(340);
				create(objBook);
				// create(objBook1);
				readAll();

			}

		/**
		 * To query all details of a book
		 * 
		 * @return
		 */
		public static List<Book> readAll()
			{
				Session session = SessionManager.getSessionfactory()
						.openSession();
				session.beginTransaction();
				List<Book> booksList = session.createQuery("from Book").list();
				session.beginTransaction().commit();
				return booksList;
			}

		public static void create(Book[] book)
			{
				Session session = SessionManager.getSessionfactory()
						.openSession();

				session.beginTransaction();
				for (int i = 0; i < book.length; i++)
					{
						Object ob = (Object) book[i];
						session.saveOrUpdate(ob);
					}

				session.flush();
				Statistics stat = SessionManager.getSessionfactory()
						.getStatistics();
				stat.setStatisticsEnabled(true);
				double queryCacheHitCount = stat.getQueryCacheHitCount();
				double queryCacheMissCount = stat.getQueryCacheMissCount();
				double queryRatio = queryCacheHitCount
						/ (queryCacheHitCount + queryCacheMissCount);
				System.out
						.println("++++++++++++++++++++++++++++++++++++++++++++"
								+ queryRatio);
				stat.logSummary();
				session.beginTransaction().commit();
			}
	}
